package com.lange.autocode.sysManager.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.PasswordUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.IdWorker;
import cn.net.autocode.platform.file.service.FileServ;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Service;

@Service
public class UserServ {
    private final Dao dao;

    private final FileServ fileServ;

    public UserServ(Dao dao, FileServ fileServ) {
        this.dao = dao;
        this.fileServ = fileServ;
    }


    /**
     * 添加用户
     */
    public ResultT addUser(JSONObject param){
        String username=param.getString("username").trim().toUpperCase();
        //判断用户名是否存在
        int exists=dao.queryValue("select count(*) from sys_users where username=?", Integer.class,username);
        if(exists>0){
            return ResultT.fail(param.getString("username")+"已经存在");
        }

        //邮箱、手机号格式验证
        String email = param.getString("email");
        String phone = param.getString("phone");
        if(!StrUtil.isBlank(email)){
            if(!Validator.isEmail(email.trim())){
                return ResultT.fail("邮箱地址无效！");
            }
            int emailExists = dao.queryValue("select count(*) from sys_users where email=?",Integer.class,email.trim());
            if (emailExists>0){
                return ResultT.fail("邮箱地址"+email+"已存在!");
            }
        }

        if(!StrUtil.isBlank(phone)){
            if(!Validator.isMobile(phone.trim())){
                return ResultT.fail("手机号格式错误！");
            }
            int phoneExists = dao.queryValue("select count(*) from sys_users where phone=?",Integer.class,phone.trim());
            if (phoneExists>0){
                return ResultT.fail("手机号"+phone+"已存在!");
            }
        }

        //初始化密码
        param.put("username",username);
        param.put("passwrd", PasswordUtils.hashpw("a1234567"));
        param.put("status","Active");

        //添加用户
        param.put("id", IdWorker.ulid());

        dao.createJSON(param, "sys_users");

        return ResultT.success(param);
    }


    public void deleteUser(JSONObject param){
        String username=param.getString("username").toUpperCase();
        //判断用户名是否存在
        JSONObject user=dao.queryJSON("select * from sys_users where username=?",username);
        if(user.isEmpty()){
            return;
        }
        //删除角色
        dao.executeUpdate("delete from sys_role_users where username=?",username);

        //删除部门用户
        dao.executeUpdate("delete from sys_department_user where username=?",username);

        //删除所属组织机构
        dao.executeUpdate("delete from sys_organ_users where username=?",username);

        dao.executeUpdate("delete from sys_user_log where username=?",username);

        dao.executeUpdate("delete from sys_users where username=?",username);

        //删除签名信息
        if(StrUtil.isNotBlank(user.getString("imageId"))){
            fileServ.delFile(user.getString("imageId"));
        }
    }

    public ResultT modifyPassword(String userName,String password) {
        if((StrUtil.isBlank(userName))||StrUtil.isBlank(password)){
            return ResultT.fail("用户或密码不能为空！");
        }
        JSONObject passPolicy = dao.queryJSON("select * from autocode_pwpolicy");
        JSONObject rs = PasswordUtils.validate(passPolicy,password);
        if(rs.getIntValue("code")==0){
            return ResultT.fail(rs.getString("msg"));
        }else{
            String hashed = PasswordUtils.hashpw(password);
            dao.executeUpdate("update sys_users set passwrd=? where username=?",hashed,userName);
            return ResultT.success();
        }
    }
}
